using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using TVDesigner.GUI.Elements;
using TVDesigner.Entities.Elements;

using Screen = TVDesigner.Entities.Elements.Screen;

namespace TVDesigner.GUI.Controls
{
    public partial class ScreensPanel : UserControl
    {
        private List<ScreenElement> _ScreenElements;
        private ScreenElement _FocusScreen;

        private FMain _MainForm;

        public FMain MainFrom
        {
            get { return _MainForm; }
            set { _MainForm = value; }
        }


        public ScreensPanel()
        {
            InitializeComponent();
            _ScreenElements = new List<ScreenElement>();

            Image im = TVDesigner.GUI.Properties.Resources.startImage;
            _StartPoint.Size = im.Size;
            PictureBox picture = _StartPoint.FocusControl as PictureBox;
            picture.Image = im;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (!Visible) return;

            if (_MainForm.Project.StartScreen != null)
            {
                foreach (Screen s in _MainForm.Project.Screens)
                {
                    if (s.Name.Equals(_MainForm.Project.StartScreen))
                    {
                        Point p1 = new Point(s.Location.X + (s.Size.Width / 2), s.Location.Y + (s.Size.Height / 2));
                        Point p2 = new Point(_StartPoint.Location.X + (_StartPoint.Width / 2), _StartPoint.Location.Y + (_StartPoint.Height / 2));
                        e.Graphics.DrawLine(new Pen(Brushes.Purple, 5), p1, p2);
                    }
                }
            }

            foreach (ScreenElement ele in _ScreenElements)
            {
                TVDesigner.Entities.Elements.Screen s = ele.LogicElement as TVDesigner.Entities.Elements.Screen;

                List<string> screensName = s.GetConnectedScreenNames();

                foreach (string name in screensName)
                {
                    TVDesigner.Entities.Elements.Screen paintScreen = _MainForm.Project.GetScreenByName(name);

                    if (paintScreen != null)
                    {
                        Point p1 = new Point(ele.Location.X + (ele.Width/2), ele.Location.Y + (ele.Height/2));
                        Point p2 = new Point(paintScreen.Location.X + (paintScreen.Size.Width / 2), paintScreen.Location.Y + (paintScreen.Size.Height / 2));
                        Point[] points = new Point[2];
                        points[0] = p1;
                        points[1] = p2;
                        e.Graphics.DrawCurve(new Pen(Brushes.Black, 5), points);
                    }
                }
            }

            base.OnPaint(e);

            if (_FocusScreen != null && !_FocusScreen.IsDragging)
            {
                Rectangle rect = new Rectangle(
                    _FocusScreen.Location.X - 2,
                    _FocusScreen.Location.Y - 2,
                    _FocusScreen.Width + 2,
                    _FocusScreen.Height + 2);

                e.Graphics.DrawRectangle(new Pen(Brushes.Black, 3), rect);
            }
        }

        protected void _Element_MouseDown(object sender, MouseEventArgs e)
        {
            Control c = sender as Control;
            _FocusScreen = c.Parent as ScreenElement;
            Invalidate();
        }

        private void ScreensPanel_VisibleChanged(object sender, EventArgs e)
        {
            if (!Visible || _MainForm == null || _MainForm.Project == null) return;

            this.BringToFront();

            _ScreenElements.Clear();
            this.Controls.Clear();
            this.Controls.Add(_StartPoint);
            foreach (TVDesigner.Entities.Elements.Screen sc in _MainForm.Project.Screens)
            {
                ScreenElement ele = new ScreenElement(sc);
                ele.AddClickEvent(_Element_MouseDown);
                this.Controls.Add(ele);
                _ScreenElements.Add(ele);
            }
        }

        private void ScreensPanel_Click(object sender, EventArgs e)
        {
            _FocusScreen = null;
            Invalidate();
        }
    }
}
